﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyBepAnTuThien
{
    public partial class UcQuanLyNguyenLieuDungCu : UserControl
    {
        public UcQuanLyNguyenLieuDungCu()
        {
            InitializeComponent();
        }
        connect cn = new connect();
        bool them = true;
        int vitri;
        private void LoadData()
        {
            gridControlDungCu.DataSource = cn.getDataTable("tblDungCu_NguyenLieu_DS");
        }
        private void UcQuanLyNguyenLieuDungCu_Load(object sender, EventArgs e)
        {
            LoadData();
            panel.Enabled = false;
            BatTatButton(true);
        }

        private void gridViewDungCu_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            txtMaSo.Text = gridViewDungCu.GetFocusedRowCellDisplayText("MaDungCu");
            txtTen.Text = gridViewDungCu.GetFocusedRowCellDisplayText("TenDungCu");
            cobTinhTrang.Text = gridViewDungCu.GetFocusedRowCellDisplayText("TinhTrang");
            cobLoai.Text = gridViewDungCu.GetFocusedRowCellDisplayText("Loai");
            cobDVT.Text = gridViewDungCu.GetFocusedRowCellDisplayText("DVT");
            txtSoLuong.Text = gridViewDungCu.GetFocusedRowCellDisplayText("SoLuong");
        }

        private void gridViewDungCu_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle == gridViewDungCu.FocusedRowHandle)
            {
                e.Appearance.BackColor = Color.DeepSkyBlue;
            }
        }
        private void BatTatButton(bool val)
        {
            btnLuu.Enabled = !val;
            btnSua.Enabled = val;
            btnThem.Enabled = val;
            btnxoa.Enabled = val;
            gridControlDungCu.Enabled = val;
        }
        private void XoaTextBox()
        {
            foreach (Control cr in panel.Controls)
            {

                if (cr.GetType() == typeof(TextBox))
                {
                    TextBox txt = (TextBox)cr;
                    txt.Clear();
                }
                if (cr.GetType() == typeof(ComboBox))
                {
                    ComboBox cob = (ComboBox)cr;
                    cob.Text = "";
                }
            }
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            them = true;
            panel.Enabled = true;
            BatTatButton(false);
            XoaTextBox();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            them = false;
            panel.Enabled = true;
            BatTatButton(false);
            vitri = (int)gridViewDungCu.FocusedRowHandle;            
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (them == true)
            {
                if (txtTen.Text == "")
                {
                    MessageBox.Show("Bạn chưa nhập tên của dụng cụ hoặc nguyên liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtTen.Focus();
                }
                else
                {
                    SqlCommand cmd_them = new SqlCommand("tblDungCu_NguyenLieu_Them", cn.ketnoi());
                    cmd_them.CommandType = CommandType.StoredProcedure;
                    cmd_them.Parameters.Add(new SqlParameter("ten", SqlDbType.NVarChar)).Value = txtTen.Text;
                    cmd_them.Parameters.Add(new SqlParameter("tinhtrang", SqlDbType.NVarChar)).Value = cobTinhTrang.Text;
                    int sl = 0;
                    if (txtMaSo.Text == "")
                    {
                        sl = 0;
                    }
                    else
                    {
                        sl = Convert.ToInt32(txtSoLuong.Text);
                    }
                    cmd_them.Parameters.Add(new SqlParameter("soluong", SqlDbType.Int)).Value = sl;
                    cmd_them.Parameters.Add(new SqlParameter("dvt", SqlDbType.NVarChar)).Value = cobDVT.Text;
                    cmd_them.Parameters.Add(new SqlParameter("loai", SqlDbType.NVarChar)).Value = cobLoai.Text;
                    if (cmd_them.ExecuteNonQuery() == 1)
                    {
                        UcQuanLyNguyenLieuDungCu_Load(sender, e);
                    }
                    else
                    {
                        MessageBox.Show("Đã xảy ra lỗi trong quá trình thêm dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        UcQuanLyNguyenLieuDungCu_Load(sender, e);                        
                    }
                }

            }
            else
            {
                SqlCommand cmd_sua = new SqlCommand("tblDungCu_NguyenLieu_Sua", cn.ketnoi());
                cmd_sua.CommandType = CommandType.StoredProcedure;
                cmd_sua.Parameters.Add(new SqlParameter("ten", SqlDbType.NVarChar)).Value = txtTen.Text;
                cmd_sua.Parameters.Add(new SqlParameter("tinhtrang", SqlDbType.NVarChar)).Value = cobTinhTrang.Text;
                int sl = 0;
                if (txtMaSo.Text == "")
                {
                    sl = 0;
                }
                else
                {
                    sl = Convert.ToInt32(txtSoLuong.Text);
                }
                cmd_sua.Parameters.Add(new SqlParameter("soluong", SqlDbType.Int)).Value = sl;
                cmd_sua.Parameters.Add(new SqlParameter("dvt", SqlDbType.NVarChar)).Value = cobDVT.Text;
                //cmd_sua.Parameters.Add(new SqlParameter("loai", SqlDbType.NVarChar)).Value = cobLoai.Text;
                cmd_sua.Parameters.Add(new SqlParameter("maso", SqlDbType.NVarChar)).Value = txtMaSo.Text;
                if (cmd_sua.ExecuteNonQuery() == 1)
                {
                    UcQuanLyNguyenLieuDungCu_Load(sender, e);
                    gridViewDungCu.FocusedRowHandle = vitri;
                }
                else
                {
                    MessageBox.Show("Đã xảy ra lỗi trong quá trình cập nhật dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    UcQuanLyNguyenLieuDungCu_Load(sender, e);
                    
                }

            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {            
            if(MessageBox.Show("Bạn có chắc chắn muốn xóa dữ liệu đang được chọn này hay không?","Thông Báo",MessageBoxButtons.YesNo,MessageBoxIcon.Warning)==DialogResult.Yes)
            {
                SqlCommand cmd = new SqlCommand("tblDungCu_NguyenLieu_Xoa", cn.ketnoi());
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("maso", SqlDbType.NVarChar)).Value = txtMaSo.Text;
                if (cmd.ExecuteNonQuery() == 1)
                {
                    UcQuanLyNguyenLieuDungCu_Load(sender, e);
                    MessageBox.Show("Đã xóa dữ liệu thành công","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    
                }
            }
        }

        private void txtTimKiem_Enter(object sender, EventArgs e)
        {
            if (txtTimKiem.Text == "Nhập từ khóa tìm kiếm")
            {
                txtTimKiem.Text = "";
            }
        }

        private void txtTimKiem_Leave(object sender, EventArgs e)
        {
            if (txtTimKiem.Text == "")
            {
                txtTimKiem.Text = "Nhập từ khóa tìm kiếm";
            }
        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            if (txtTimKiem.Text != "Nhập từ khóa tìm kiếm")
            {
               // gridViewDungCu.Columns.Clear();
                gridViewDungCu.OptionsBehavior.Editable = false;
                SqlCommand cmd = new SqlCommand("tblDungCu_NguyenLieu_TimKiem",cn.ketnoi());
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("tim",SqlDbType.NVarChar)).Value = txtTimKiem.Text;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                gridControlDungCu.DataSource = dt;

            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtTimKiem.Text = "Nhập từ khóa tìm kiếm";
            UcQuanLyNguyenLieuDungCu_Load(sender, e);
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            UcQuanLyNguyenLieuDungCu_Load(sender, e);           
            gridViewDungCu.FocusedRowHandle = 0;
        }

        private void btnInDS_Click(object sender, EventArgs e)
        {
            frmInDungCuNguyenLieu _frm = new frmInDungCuNguyenLieu();
            _frm.ShowDialog();
        }

        private void gridControlDungCu_Click(object sender, EventArgs e)
        {

        }

    }
}
